package com.lj.controller;

import com.lj.pojo.Pmanager;
import com.lj.pojo.Stafflj;
import com.lj.service.PmanagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/pManager")
public class PmanagerAction {
    @Autowired
    PmanagerService pmanagerService;
    @RequestMapping("/select")
    public String select(String id, HttpServletRequest request){
        Pmanager pmanager= pmanagerService.select(id);
        request.setAttribute("pManager",pmanager);
        return "manager/pManagerView";
    }
    @RequestMapping("/update")
    public String update(RedirectAttributes attributes,String managerPassword, String managerName, Byte managerAge, String managerSex, String managerPhone, String managerId){
        int nums= pmanagerService.update(managerPassword,managerName,managerAge,managerSex,managerPhone,managerId);
        attributes.addAttribute("id",managerId);
        return "redirect:/pManager/select.action";
    }
    @RequestMapping("/selectStaff")
    public String selectStaff(String departmentName,HttpServletRequest request){
        List<Stafflj> staffljList= pmanagerService.selectStaff(departmentName);
        request.setAttribute("staffList",staffljList);
        return "manager/staffMView";
    }
    @RequestMapping("/insertStaff")
    public String insertStaff(Stafflj stafflj,RedirectAttributes attributes,HttpServletRequest request){
        if(stafflj.getClientId().equals("null")){
            stafflj.setClientId(null);
        }
        if(stafflj.getDepartmentName().equals("null")){
            stafflj.setDepartmentName(null);
        }
        if(stafflj.getProjectName().equals("null")){
            stafflj.setProjectName(null);
        }
        int num=-1;
        String departmentName=(String) request.getSession().getAttribute("departmentName");
        num=pmanagerService.insertStaff(stafflj);
        attributes.addAttribute("departmentName",departmentName);
        return "redirect:/pManager/selectStaff.action";
    }
    @RequestMapping("/deleteStaff")
    public String deleteStaff(RedirectAttributes attributes,HttpServletRequest request,String staffId){
        int nums=-1;
        nums=pmanagerService.deleteStaff(staffId);
        String departmentName=(String) request.getSession().getAttribute("departmentName");
        attributes.addAttribute("departmentName",departmentName);
        return"redirect:/pManager/selectStaff.action";
    }
    @ResponseBody
    @RequestMapping("/getStaffById")
    public Stafflj getStaffById(String staffId){
        return pmanagerService.getById(staffId);
    }
    @RequestMapping("/updateStaff")
    public String update(RedirectAttributes attributes,HttpServletRequest request,Stafflj stafflj){
        if(stafflj.getProjectName().equals("null")){
            stafflj.setProjectName(null);
        }
        if(stafflj.getClientId().equals("null")){
            stafflj.setClientId(null);
        }
        if(stafflj.getDepartmentName().equals("null")){
            stafflj.setDepartmentName(null);
        }
        String departmentName=(String) request.getSession().getAttribute("departmentName");
        attributes.addAttribute("departmentName",departmentName);
        int nums= pmanagerService.update(stafflj);
        return"redirect:/pManager/selectStaff.action";
    }
}
